package com.baymax.test.scene.demo

import android.graphics.PointF
import android.os.Bundle
import android.view.InputDevice
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newSceneFlexContainer
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class MouseScene : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        val f = MutableStateFlow(PointF(0f ,0f))
        return p1.newSceneFlexContainer {
            p1.setOnGenericMotionListener { v, event ->
                if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
                    f.value = PointF(event.x , event.y)
                }
                false
            }

            val t = addButton("")
            launch {
                f.collectLatest {  t.text = "x=${it.x},y=${it.y}"  }
            }
        }
    }

}